package com.cqrt.controller;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.dto.RTImportTaskDTO;
import com.cqrt.service.RTTaskService;
import com.cqrt.vo.RTCheckTableVO;
import com.cqrt.vo.RTTaskVO;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.model.dto.job.JobRemoteDTO;
import com.hustcad.plm.rpm.model.dto.job.TyppmJobDTO;
import com.hustcad.plm.rpm.model.vo.job.JobExtVO;
import com.hustcad.plm.rpm.service.job.TyppmJobService;
import com.hustcad.plm.rpm.util.beanmapper.BeanMapper;
import com.ty.basic.response.ResponseResult;
import com.ty.ppm.model.constant.ApiPathConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;


@RestController
@Api(tags = "任务")
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH + "rtTask")
public class RTTaskController {


    @Resource
    private RTTaskService rtTaskService;
    @Resource
    private TyppmJobService typpmJobService;


    @PostMapping("/batchImportTask")
    @ApiOperation(value = "batchImportTask",notes = "分解子任务批量导入")
    @ResponseBody
    public ResponseResult<List<TyppmJobDTO>> batchImportTask(@ModelAttribute RTImportTaskDTO dto) {
        List<TyppmJobDTO> jobDTOS = rtTaskService.batchImportTask(dto);
        return ResponseResult.success(jobDTOS);
    }

    @PutMapping({"/addTemporaryJob"})
    @ApiOperation(
            value = "新增临时任务",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<JobExtVO> insertJob(@RequestBody JobExtVO job) {
        this.typpmJobService.checkCreateJobParams(job);
        TyppmJobDTO map =  BeanMapper.map(job, TyppmJobDTO.class);
        rtTaskService.addTemporaryJob(map, map.getCheckItemList(), map.getDeliverRemoteList(), true);
        this.typpmJobService.calculate(map);
        this.typpmJobService.insertMeetingObjLink(job);
        return TyppmResponseResult.success(job);
    }

    @GetMapping("/checkTaskOutRule")
    @ApiOperation(value = "检查任务的交付物规则", produces = "application/json; charset=utf-8")
    public ResponseResult<RTTaskVO> checkTaskOutRule(String jobId ){
        RTTaskVO rtTaskVO = rtTaskService.checkTaskOutRule(jobId);
        return ResponseResult.success(rtTaskVO);
    }


    @GetMapping("/checkCompleteOutRule")
    @ApiOperation(value = "齐套性检查交付物规则", produces = "application/json; charset=utf-8")
    public ResponseResult<List<RTCheckTableVO>> checkCompleteOutRule(String projectOid){
        List<RTCheckTableVO> rtTaskVO = rtTaskService.checkCompleteOutRule(new BigInteger(projectOid));
        return ResponseResult.success(rtTaskVO);
    }

    @GetMapping("/checkSubmitOutRule")
    @ApiOperation(value = "绑定交付物规则提示用户是否提交交付物", produces = "application/json; charset=utf-8")
    public ResponseResult<Boolean> checkSubmitOutRule(String jobOid){
        boolean submit = rtTaskService.checkSubmitOutRule(jobOid);
        return ResponseResult.success(submit);
    }



    @PostMapping("/insertOutRole")
    @ApiOperation(value = "绑定交付物规则提示用户是否提交交付物", produces = "application/json; charset=utf-8")
    public ResponseResult<Boolean> insertOutRole(@RequestBody JSONObject jsonObject){
        try {
            rtTaskService.insertOutRole(jsonObject);
        }catch (Exception e){
            return ResponseResult.fail("400",e.getMessage());
        }

        return ResponseResult.success();
    }

    @GetMapping("/updateJobStatus")
    @ApiOperation(value = "更新任务状态", produces = "application/json; charset=utf-8")
    public ResponseResult<Boolean> updateJobStatus(@RequestParam String oid, @RequestParam String riskType) {

        boolean update = rtTaskService.updateJobStatus(oid, riskType);

        return ResponseResult.success(update);
    }



    @PostMapping("/createTemporaryJob")
    @ApiOperation(value = "新建临时任务", produces = "application/json; charset=utf-8")
    public ResponseResult createTemporaryJob(@RequestBody JobExtVO job) {
        rtTaskService.createTemporaryJob(job);
        return ResponseResult.success();
    }


}
